package com.bruce.dao.impl;

import com.bruce.bean.Colleage;
import com.bruce.bean.Course;
import com.bruce.dao.CollegeDao;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cc
 * @date 2021/7/20 18:45
 * @Description
 */

public class CollegeDaoImpl extends BaseDao implements CollegeDao {
    @Override
    public List<Colleage> findColleges() {
        List<Colleage> query = new ArrayList<>();

        try {
            query = queryRunner.query("select * from college", new BeanListHandler<Colleage>(Colleage.class));
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return query;
    }

    @Override
    public int insertCourse(Course course) {
        String sql = "insert into course values(?,?,?,?,?,?) ";
        Object[] params = {course.getCid(),course.getCname(),course.getCintroduction(),course.getType(),course.getBelongcoll(),course.getBelongpro()};
        int i=0;
        try {
             i = queryRunner.update(sql, params);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return i;
    }

    @Override
    public String findcollegestitle(int parseInt) {
        String sql = "select mname from major where id=?";
        String title=null;
        try {
            title= queryRunner.query(sql,new ScalarHandler<>(),parseInt);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return title;
    }
}
